package com.xxxx;

import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.configuration.Configuration;

/**
 * 批量数据处理
 */
public class Hello12FlinkRich {
    public static void main(String[] args) throws Exception {
        //创建环境
        ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
        //获取数据源
        DataSource<Integer> source = environment.fromElements(1, 2, 3, 4, 5, 6, 7);
        //过滤
        source.filter(new RichFilterFunction<Integer>() {

            int random = 0;

            @Override
            public void open(Configuration parameters) throws Exception {
                //开启连接
                random = (int) (Math.random() * 100000);
            }

            @Override
            public void close() throws Exception {
                //关闭连接
                random = 0;
            }

            @Override
            public boolean filter(Integer num) throws Exception {
                return random % 2 == 0;
            }
        }).print();

    }
}
